﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ATM
{
    class ATM
    {
        State doneState, hasEnvelopeState, depositeState, transferState, 
              cashState, hasPinState, hasCardState, noCardState;
        State curState;
        Account acc;

        string[] lines;
        string[] data;
        int[] storedPins = new int[2];

        public State CurrentState
        {
            get { return curState; }
        }

        public ATM()
        {
            doneState = new DoneState(this);
            hasEnvelopeState = new HasEnvelopeState(this);
            depositeState = new DepositState(this);
            transferState = new TransferState(this);
            cashState = new CashState(this);
            hasPinState = new HasPINState(this);
            hasCardState = new HasCardState(this);
            noCardState = new NoCardState(this);
            acc = new Account();

            curState = noCardState;

            lines = File.ReadAllLines("Bank Data.csv");
            int count = 0;
            foreach (string rawData in lines)
            {
                data = rawData.Split(',');
                for (int i = 0; i < data.Length; i++)
                {
                    if (i % 2 != 0)
                    {
                        storedPins[count] = int.Parse(data[i]);
                    }
                }
                count++;
            }
        }

        public State GetDoneState()
        {
            return doneState;
        }
        public State GetHasEnvelopState()
        {
            return hasEnvelopeState;
        }
        public State GetDepositState()
        {
            return depositeState;
        }
        public State GetTransferState()
        {
            return transferState;
        }
        public State GetCashState()
        {
            return cashState;
        }
        public State GetHasPinState()
        {
            return hasPinState;
        }
        public State GetHasCardState()
        {
            return hasCardState;
        }
        public State GetNoCardState()
        {
            return noCardState;
        }
        public Account GetAccount()
        {
            return acc;
        }

        public void SetState(State state)
        {
            curState = state;
        }

        public string InsertCard()
        {
            return curState.InsertCard();
        }

        public string EjectCard()
        {
            return curState.EjectCard();
        }

        public void EnterAmount()
        {
            throw new NotImplementedException();
        }

        public bool ReturnMainMenu()
        {
             return curState.EnterReturnToMenu();
        }

        public bool EnterPIN(int pin)
        {
            bool passPINCheck;
            passPINCheck = curState.EnterPIN(pin, storedPins);
            return passPINCheck;
        }

        public bool SelectDeposite()
        {
            return curState.ChooseDeposite();
        }

        public void SelectTransfer()
        { }

        public bool SelectGetCash()
        {
            return curState.ChooseCash();
        }
        public bool InsertEnvelope()
        {
            return curState.InsertEnvelope();
        }
        public void Deposit(double amount)
        {
            acc.Deposit(amount);
            curState.EnterAmount();
        }
        public void Withdraw(double amount)
        {
            acc.Withdraw(amount);
            curState.EnterAmount();
        }
    }
}
